<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    
  </style>
</head>
<body>
  <h2>请输入新用户信息</h2>
  姓名：<input type="text" class="uName"><br>
  手机号：<input type="text" class="tel"><br>
  权限：<select class="power">
    <option value="0">超管</option>
    <option value="1">经理</option>
    <option value="2">员工</option>
  </select><br>
  <input type="button" value="添加" class="add">
  <a href="./index.html">回到首页</a>
</body>
<script src="../ajax.js"></script>
<script>
  
  const userData = JSON.parse(localStorage.getItem("userData")) || {};

  $(".add").onclick = function(){
    ajax({
      url: "http://10.11.55.86:3000/adduser",
      data:{
        token: userData.token,
        uName: $(".uName").value,
        tel: $(".tel").value,
        power: $(".power").value
      },
      method:"post",
      success(xhr){
        const res = JSON.parse(xhr.responseText)
        if(res.code === 2){
          if(!confirm("添加成功，是否继续添加")){
            location.assign("./index.html")
          }else{
            $(".uName").value = "";
            $(".tel").value = "";
            $(".uName").focus();
          }
        }else if(res.code === 0){
          alert("登录失效，请重新登录");
          location.assign("./login.html")
        }else{
          alert(res.msg);
        }
      }
    })
  }


  function $(s){
    return document.querySelector(s)
  }
</script>
</html>